package com.erlang.springmvc.controller;

import com.erlang.springmvc.exception.ProjectException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @ControllerAdvice作用：
 *    为controller添加增强效果处理
 * @Description：
 */
@ControllerAdvice
public class BaseController {
    /** 基于@ExceptionHandler异常处理 */
    @ExceptionHandler
    public String ExceptionHandler(HttpServletRequest request, Exception ex, Model model) {

        ProjectException projectException = null;
        if (ex instanceof ProjectException){
            projectException = (ProjectException) ex;
        }else {
            projectException = new ProjectException("未知异常", "-1");
        }
        model.addAttribute("message", projectException.getMessage());
        model.addAttribute("code", projectException.getCode());
        return "error";
    }
}